package com.example.huangziwei.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by huangziwei on 16-4-26.
 */
public class TestImageView extends LinearLayout {
    private Matrix mMatrix = new Matrix();
    private PointF mLastTouch;
    private Paint mPaint;
    private Path mPath;
    private Path mPathBack;
    private Bitmap mBitmap;
    private Canvas mCanvas;

    private PointF mRightBottom, mCornorTouchToRigBot, mBottomHelper, mRightHelper;

    private float mBackRoateDegree;

    public TestImageView(Context context) {
        this(context, null);
    }

    public TestImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(20);

        mLastTouch = new PointF();
        mRightBottom = new PointF();
        mCornorTouchToRigBot = new PointF();
        mBottomHelper = new PointF();
        mRightHelper = new PointF();

        mPath = new Path();
        mPathBack = new Path();

        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mCanvas = new Canvas();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRightBottom.x = w;
        mRightBottom.y = h;

        mLastTouch.x = mRightBottom.x;
        mLastTouch.y = mRightBottom.y;

        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        mCanvas.setBitmap(mBitmap);
        calculate();
        invalidate();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
//        mMatrix.reset();
////        mMatrix.setRotate(45);
//        mMatrix.postTranslate(-getWidth() / 2, -getHeight() / 2);
//        mMatrix.postRotate(45);
//        mMatrix.postTranslate(getWidth() / 2, getHeight() / 2);
////        mMatrix.postSkew(0, 0, 50, 50);
//        // 画出变换后的图像
//        canvas.drawBitmap(((BitmapDrawable) getDrawable()).getBitmap(), mMatrix, null);
//
//        mPaint.setColor(Color.RED);
//        canvas.drawCircle(mLastTouch.x, mLastTouch.y, 10, mPaint);
//        canvas.drawCircle(mCornorTouchToRigBot.x, mCornorTouchToRigBot.y, 10, mPaint);
//        canvas.drawCircle(mRightBottom.x, mRightBottom.y, 10, mPaint);
//        mPaint.setColor(Color.BLUE);
//        canvas.drawCircle(mBottomHelper.x, mBottomHelper.y, 10, mPaint);
//        canvas.drawCircle(mRightHelper.x, mRightHelper.y, 10, mPaint);

        canvas.save();
        canvas.clipPath(mPath);
        super.dispatchDraw(canvas);
        canvas.restore();

        canvas.save();
        canvas.clipPath(mPathBack);


        canvas.translate(mLastTouch.x - mRightBottom.x, mLastTouch.y);
        canvas.rotate(-90 + mBackRoateDegree, getMeasuredWidth(), 0);
        canvas.translate(getWidth() / 2, getHeight() / 2);
        canvas.scale(1, -1);
        canvas.translate(-getWidth() / 2, -getHeight() / 2);


        if (Build.VERSION.SDK_INT >= 21) {
            canvas.saveLayerAlpha(0, 0, getWidth(), getHeight(), 50);
        }
        super.dispatchDraw(canvas);
        canvas.restore();
    }

    private void calculate() {
        if(mLastTouch.y>mRightBottom.y){
            mLastTouch.y = mRightBottom.y-0.5f;
        }
        if(mLastTouch.x>mRightBottom.x){
            mLastTouch.x = mRightBottom.x-0.5f;
        }
        mCornorTouchToRigBot.x = (mLastTouch.x + mRightBottom.x) / 2;
        mCornorTouchToRigBot.y = (mLastTouch.y + mRightBottom.y) / 2;

        float height = (float) Math.sqrt((mRightBottom.x - mCornorTouchToRigBot.x) * (mRightBottom.x - mCornorTouchToRigBot.x)
                + (mRightBottom.y - mCornorTouchToRigBot.y) * (mRightBottom.y - mCornorTouchToRigBot.y));
        float x;
        if (height == 0) {
            x = 0;
        } else {
            x = height / (mRightBottom.x - mCornorTouchToRigBot.x) * height;
        }
        mBottomHelper.x = mRightBottom.x - x;
        mBottomHelper.y = mRightBottom.y;
        float height2 = (float) Math.sqrt(x * x - height * height);
        float y;
        if (height2 == 0) {
            y = 0;
        } else {
            y = x / height2 * height;
        }
        mRightHelper.x = mRightBottom.x;
        mRightHelper.y = mRightBottom.y - y;

        mPath.reset();
        mPath.moveTo(0, 0);
        mPath.lineTo(mRightBottom.x, 0);
        mPath.lineTo(mRightHelper.x, mRightHelper.y);
        mPath.lineTo(mLastTouch.x, mLastTouch.y);
        mPath.lineTo(mBottomHelper.x, mBottomHelper.y);
        mPath.lineTo(0, mRightBottom.y);
        mPath.close();

        mPathBack.reset();
        mPathBack.moveTo(mBottomHelper.x, mBottomHelper.y);
        mPathBack.lineTo(mLastTouch.x, mLastTouch.y);
        mPathBack.lineTo(mRightHelper.x, mRightHelper.y);
        mPathBack.close();

        float d = (float) Math.toDegrees(Math.atan(
                (mRightBottom.y - mLastTouch.y) / (mLastTouch.x - mBottomHelper.x)));
        if (d < 0) {
            d = -90 - d;
            mBackRoateDegree = d;
        } else {
            mBackRoateDegree = 90 - d;
        }
        Log.i("hzw", mBackRoateDegree + "　" + d);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastTouch.x = event.getX();
                mLastTouch.y = event.getY();
                calculate();
                invalidate();
                return true;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                mLastTouch.x = event.getX();
                mLastTouch.y = event.getY();
                calculate();
                invalidate();
                return true;
            case MotionEvent.ACTION_MOVE:
                mLastTouch.x = event.getX();
                mLastTouch.y = event.getY();
                calculate();
                invalidate();
                return true;
        }
        return super.onTouchEvent(event);
    }
}
